package com.yanqu.road.server.manager.cross.battle.ghost;

import com.yanqu.road.entity.activity.ghost.GhostSimplePlayer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 鬼市跨服场景
 */
public class GhostCrossSceneLogic {

    private final Object lockRoomObject = new Object();

    // BossId，服务器Id，用户Id，数据
    private Map<Integer, Map<Long, Map<Long, GhostSimplePlayer>>> bossRoomMap = new ConcurrentHashMap<>();

    // 服务器Id，用户Id，BossId
    private Map<Long, Map<Long, Integer>> userRoomMap = new ConcurrentHashMap<>();

    /**
     * 初始化房间
     */
    public void initRoom(long serverId, int bossId) {
        if (!bossRoomMap.containsKey(bossId)) {
            bossRoomMap.put(bossId, new ConcurrentHashMap<>());
        }
        if (!bossRoomMap.get(bossId).containsKey(serverId)) {
            bossRoomMap.get(bossId).put(serverId, new ConcurrentHashMap<>());
        }

        if (!userRoomMap.containsKey(serverId)) {
            userRoomMap.put(serverId, new ConcurrentHashMap<>());
        }
    }

    /**
     * 进入房间
     */
    public void enterRoom(GhostSimplePlayer player, int bossId) {
        synchronized (lockRoomObject) {
            initRoom(player.getServerId(), bossId);
            // 旧房间移除
            Integer oldBossId = userRoomMap.get(player.getServerId()).get(player.getUserId());
            if (oldBossId != null) {
                bossRoomMap.get(oldBossId).get(player.getServerId()).remove(player.getUserId());
            }
            // 加新房间
            userRoomMap.get(player.getServerId()).put(player.getUserId(), bossId);
            bossRoomMap.get(bossId).get(player.getServerId()).put(player.getUserId(), player);
        }
    }

    /**
     * 退出房间
     *
     * @return bossId，没有就是0
     */
    public int quitRoom(GhostSimplePlayer player) {
        synchronized (lockRoomObject) {
            if (!userRoomMap.containsKey(player.getServerId())) {
                return 0;
            }
            // 判断是否在房间
            Integer oldBossId = userRoomMap.get(player.getServerId()).get(player.getUserId());
            if (oldBossId == null) {
                return 0;
            }
            // 移除
            userRoomMap.get(player.getServerId()).remove(player.getUserId());
            bossRoomMap.get(oldBossId).get(player.getServerId()).remove(player.getUserId());
            return oldBossId;
        }
    }

    /**
     * 获取房间内玩家
     *
     * @param serverId 优先获取该区服的数据
     * @param userId   不获取自己
     */
    public List<GhostSimplePlayer> getRoomUserList(int bossId, long serverId, long userId, int maxCount) {
        List<GhostSimplePlayer> playerList = new ArrayList<>();
        if (!bossRoomMap.containsKey(bossId)) {
            return new ArrayList<>();
        }
        Map<Long, Map<Long, GhostSimplePlayer>> serverMap = bossRoomMap.get(bossId);
        // 优先获取自己区服的
        if (serverMap.containsKey(serverId)) {
            for (GhostSimplePlayer simplePlayer : serverMap.get(serverId).values()) {
                if (playerList.size() >= maxCount) {
                    break;
                }
                if (simplePlayer.getUserId() == userId) {
                    // 跳过自己
                    continue;
                }
                playerList.add(simplePlayer);
            }
        }
        // 其他区服的
        for (Map.Entry<Long, Map<Long, GhostSimplePlayer>> serverEntry : serverMap.entrySet()) {
            Long tmpServerId = serverEntry.getKey();
            if (tmpServerId == serverId) {
                continue;
            }
            Map<Long, GhostSimplePlayer> simplePlayerMap = serverEntry.getValue();
            for (GhostSimplePlayer simplePlayer : simplePlayerMap.values()) {
                if (playerList.size() >= maxCount) {
                    break;
                }
                playerList.add(simplePlayer);
            }
        }
        return playerList;
    }

}
